﻿using EM.OA.BLL;
using EM.OA.DataModel.DBModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;

namespace EM.OA.WebCommon.Base
{
    /// <summary>
    /// Controller基类,不带权限
    /// </summary>
    public class BaseController : Controller
    {
        protected ControllerOpResult OperationResult = new ControllerOpResult();

        /// <summary>
        /// 转换为JSON
        /// </summary>
        /// <param name="list">数据列表</param>
        /// <param name="totalCount">数据总条数,分页数据list.Count!=totalCount</param>
        /// <returns></returns>
        protected JsonResult ToJson(List<object> list, int totalCount)
        {
            Dictionary<string, object> json = new Dictionary<string, object>();
            json.Add("total", totalCount);
            json.Add("rows", list);
            return Json(json);
        }

        /// <summary>
        /// 转换为JSON
        /// </summary>
        /// <typeparam name="TEntity">数据库实体Entity</typeparam>
        /// <typeparam name="TModel">页面Model</typeparam>
        /// <param name="list">数据列表</param>
        /// <param name="totalCount">总数据数</param>
        /// <returns></returns>
        protected JsonResult ToJson<TEntity, TModel>(List<TEntity> list, int totalCount)
            where TModel : BaseViewModel, new()
            where TEntity : class ,new()
        {
            List<object> result = new List<object>();
            list.ForEach(p =>
            {
                result.Add(new TModel().ToModel<TEntity>(p));
            });
            Dictionary<string, object> json = new Dictionary<string, object>();
            json.Add("total", totalCount);
            json.Add("rows", result);
            return Json(json);
        }

        /// <summary>
        /// 查询下拉列表的键值
        /// </summary>
        /// <param name="dropdownListType"></param>
        /// <returns></returns>
        public IEnumerable<SelectListItem> GetListItems(string code, string defaultValue = null)
        {
            List<SelectListItem> items = new List<SelectListItem> { new SelectListItem { Text = "===请选择===", Value = "" } };
            DictionaryBLL bll = new DictionaryBLL();
            List<EMOA_Dictionary> list = bll.GetDicList(code);
            list.ForEach(p =>
            {
                items.Add(new SelectListItem
                {
                    Text = p.DictionaryName,
                    Value = p.DictionaryCode
                });
            });
            if (!string.IsNullOrEmpty(defaultValue))
            {
                SelectListItem item = items.FirstOrDefault(p => p.Value == defaultValue);
                if (item != null)
                {
                    item.Selected = true;
                }
            }

            return items;
        }

        /// <summary>
        /// 获取View传递过来的Post参数
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <returns></returns>
        public string GetParam(string paramName)
        {
            return Request.Params[paramName].IsNotEmpty() ? Request.Params[paramName] : "";
        }

        /// <summary>
        /// 获取View传递过来的URL参数
        /// </summary>
        /// <param name="paramName">参数名</param>
        /// <returns></returns>
        public string GetUrlParam(string paramName)
        {
            return Request.QueryString[paramName].IsNotEmpty() ? Request.QueryString[paramName] : "";
        }

    }

    public class ControllerOpResult
    {
        public CommonEnum.ViewOperation Operation { get; set; }
        public CommonEnum.ViewOpStatus OperationStatus { get; set; }
        public string ErrorMsg { get; set; }
    }
}
